package com.example.contactlist;

import java.util.ArrayList;

import db.SimpleDatabaseHelper;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.SyncStateContract.Helpers;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class List extends Activity {
	private SimpleDatabaseHelper helper = null;
	ListAdapter ada;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		helper = new SimpleDatabaseHelper(this);
		setContentView(R.layout.activity_list);
		
		ArrayList<Contact> data = new ArrayList<Contact>();
//		Contact contact = new Contact();
//		contact.setName("aaa");
//		contact.setKana("bbb");
//		contact.setAdr("ccc");
//		contact.setMail("ddd");
//		contact.setTel("000-123-1234");
//		data.add(contact);
		
		ada = new ListAdapter(this, data, R.layout.activity_list_sub);
		ListView list = (ListView) findViewById(R.id.listViewMain);
		list.setAdapter(ada);

		list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> av, View view, int position,
					long id) {
				ListAdapter adapter = (ListAdapter) av.getAdapter();
				Contact item = (Contact) adapter.getItem(position);
				Intent intent = new Intent(List.this,
						com.example.contactlist.Show.class);
				intent.putExtra("contact", item);
				startActivityForResult(intent, 1);
			}
		});
	}
	

	@Override
	protected void onResume() {
		super.onResume();
		ada.clear();
		SQLiteDatabase db = helper.getReadableDatabase();
		String[] cols={"id","name","adr","tel","mail","kana","img"};
		Cursor cs=db.query("contacts", cols, null, null, null, null, "id", null);
		boolean eol=cs.moveToFirst();
		while(eol){
			Contact contact = new Contact();
			contact.setId(cs.getLong(0));
			contact.setName(cs.getString(1));
			contact.setAdr(cs.getString(2));
			contact.setTel(cs.getString(3));
			contact.setMail(cs.getString(4));
			contact.setKana(cs.getString(5));
			contact.setImg(cs.getString(6));
			ada.add(contact);
			
			eol=cs.moveToNext();
		}
		
	}


	public void UpOnClick(View view) {
		Intent intent = new Intent(List.this,
				com.example.contactlist.Input.class);
		startActivityForResult(intent, 2);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (data !=null) {
			Contact contact =(Contact)data.getSerializableExtra("contact");
			if(requestCode == 2){
				SQLiteDatabase db = helper.getWritableDatabase();
				ContentValues cv =new ContentValues();
				cv.put("name", contact.getName());
				cv.put("adr", contact.getAdr());
				cv.put("tel", contact.getTel());
				cv.put("mail", contact.getMail());
				cv.put("kana", contact.getKana());
				cv.put("img", contact.getImg());
//idは自動的にふられるのでputしてしまうとエラーが出る				
//				cv.put("id", contact.getId());
				db.insert("contacts",null,cv);
				ada.add(contact);
			}
			else if(requestCode == 1){
				String[] params= {String.valueOf(contact.getId())};
				SQLiteDatabase db = helper.getWritableDatabase();
				db.delete("contacts","id=?", params);
			

				
			}
		}
	}
}
